package com.tomecode.tools.maven.settings.model;

/**
 * This element specifies how often updates should attempt to occur. Maven will
 * compare the local POM's timestamp (stored in a repository's maven-metadata
 * file) to the remote. The choices are: always, daily (default), interval:X
 * (where X is an integer in minutes) or never.
 * 
 * @author Frastia Tomas
 * 
 */
public enum UpdatePolicy {

	EMPTY(""), ALWAYS("always"), DAILY("daily"), INTERVAL("interval"), NEVER("never");

	private UpdatePolicy(String name) {
		this.name = name;
	}

	private String name;

	public final String toString() {
		return name;
	}

	public static final String getInterval(int interval) {
		return INTERVAL.name + ":" + interval;
	}

	public static final UpdatePolicy parse(String updatePolicy) {
		if (updatePolicy == null || updatePolicy.trim().length() == 0) {
			return UpdatePolicy.EMPTY;
		}

		int index = updatePolicy.indexOf(":");
		if (index != -1) {
			updatePolicy = updatePolicy.substring(0, index);
		}
		for (UpdatePolicy up : values()) {
			if (up.toString().equals(updatePolicy)) {
				return up;
			}
		}
		return UpdatePolicy.EMPTY;
	}

	public final static int parseInterval(String updatePolicy) {
		int index = updatePolicy.indexOf(":");
		if (index != -1) {
			try {
				return Integer.parseInt(updatePolicy.substring(index + 1));
			} catch (NumberFormatException e) {
			}
		}

		return 0;
	}
}
